package com.demo.read_word.service.impl;

import cn.afterturn.easypoi.word.WordExportUtil;
import com.demo.read_word.entity.Difficulty;
import com.demo.read_word.service.DifficultyTableService;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.springframework.stereotype.Service;
import org.springframework.util.ResourceUtils;


/**
 * @author WJL
 * @version 1.0
 * @date 2023/02/06 13:56
 * @功能描述
 **/
@Service
public class DifficultyTableServiceImpl implements DifficultyTableService {

    @Override
    public void down(HttpServletResponse response) throws Exception {
        File rootPath = new File(ResourceUtils.getURL("classpath:").getPath()); //SpringBoot项目获取根目录的方式
        File templatePath = new File(rootPath.getAbsolutePath(),"/templates/word.docx");
        //准备导出数据
        //查询所有用户数
        // 生成 Random 对象

        Difficulty difficulty = new Difficulty();
        difficulty.setYx("信息工程");
        difficulty.setZy("计科");
        difficulty.setNj("2020");
        difficulty.setBj("1");
        difficulty.setXh("123");
        difficulty.setName("肖");
        difficulty.setSex("男");
        difficulty.setBirthday("2000");
        difficulty.setJg("四川");
        difficulty.setSfzhm("2512312");
        difficulty.setJtrk("4");
        difficulty.setPhone("191");
        difficulty.setXxdz("四川省");
        difficulty.setYzbm("641300");
        difficulty.setJzsjh("13555");
        difficulty.setXf("1400");
        difficulty.setZsf("800");
        difficulty.setJzxm("xy");
        difficulty.setJznj("55");
        difficulty.setYxsgx("父子");
        difficulty.setGzdw("无");
        difficulty.setJob("无");
        difficulty.setJznsr("20");
        difficulty.setJkzt("健康");
        difficulty.setJtnsr("30000");
        difficulty.setQm("肖");
        difficulty.setN("2020");
        difficulty.setY("2");
        difficulty.setR("10");
        
       // Difficulty Difficulty= this.list().get(number);
        Map<String,Object> params = new HashMap<String,Object>();
        String yx = difficulty.getYx();
        String zy = difficulty.getZy();
        String nj = difficulty.getNj();
        String bj = difficulty.getBj();
        String xh = difficulty.getXh();
        String name = difficulty.getName();
        String sex = difficulty.getSex();
        String birthday = difficulty.getBirthday();
        String jg = difficulty.getJg();
        String sfzhm = difficulty.getSfzhm();
        String jtrk = difficulty.getJtrk();
        String phone = difficulty.getPhone();
        String xxdz = difficulty.getXxdz();
        String yzbm = difficulty.getYzbm();
        String jzsjh = difficulty.getJzsjh();
        String xf = difficulty.getXf();
        String zsf = difficulty.getZsf();
        String jzxm = difficulty.getJzxm();
        String jznj = difficulty.getJznj();
        String yxsgx = difficulty.getYxsgx();
        String gzdw = difficulty.getGzdw();
        String job = difficulty.getJob();
        String jznsr = difficulty.getJznsr();
        String jkzt = difficulty.getJkzt();
        String jtnsr = difficulty.getJtnsr();
        String qm = difficulty.getQm();
        String n = difficulty.getN();
        String y = difficulty.getY();
        String r = difficulty.getR();

        params.put("yx",yx);
        params.put("zy",zy);
        params.put("nj",nj);
        params.put("bj",bj);
        params.put("xh",xh);
        params.put("name",name);
        params.put("sex",sex);
        params.put("birthday",birthday);
        params.put("jg",jg);
        params.put("sfzhm",sfzhm);
        params.put("jtrk",jtrk);
        params.put("phone",phone);

        params.put("xxdz",xxdz);
        params.put("yzbm",yzbm);
        params.put("jzsjh",jzsjh);
        params.put("xf",xf);
        params.put("zsf",zsf);
        params.put("jzxm",jzxm);
        params.put("jznj",jznj);
        params.put("yxsgx",yxsgx);
        params.put("gzdw",gzdw);
        params.put("job",job);
        params.put("jznsr",jznsr);
        params.put("jkzt",jkzt);
        params.put("jtnsr",jtnsr);
        params.put("qm",qm);
        params.put("n",n);
        params.put("y",y);
        params.put("r",r);

        params.put("1","☑");
        params.put("2","☑");
        //根据模板+数据 导出文档
        XWPFDocument xwpfDocument = WordExportUtil.exportWord07(templatePath.getPath(), params);
        String filename="word.docx";
        //设置文件的打开方式和mime类型
        ServletOutputStream outputStream = response.getOutputStream();
        response.setHeader( "Content-Disposition", "attachment;filename="  + new String(filename.getBytes(),"ISO8859-1"));
        response.setContentType("application/vnd.openxmlformats-officedocument.wordprocessingml.document");
        xwpfDocument.write(outputStream);
    }
}
